package com.sse.controller;

import com.sse.constant.SseConstant;
import com.sse.form.SseMessageForm;
import com.sse.service.SseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;


@RestController
@RequestMapping("sse")
public class SseTestController {

    private static final Logger logger = LoggerFactory.getLogger(SseTestController.class);

    @Resource
    private SseService sseService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("start")
    public SseEmitter start(String clientId) {
        return sseService.start(clientId);
    }

    @RequestMapping("/end")
    public String close(String clientId) {
        return sseService.close(clientId);
    }

    @RequestMapping("/sendMsg")
    public void sendMsg(@RequestBody SseMessageForm sseMessageForm) {
        redisTemplate.convertAndSend(SseConstant.SSE_MESSAGE_LISTENER_CHANNEL_TOPIC, sseMessageForm);
    }

}
